#include once "inputex.bas"

sub input_cb_gfx( x as uinteger, y as uinteger, s as string, d as any ptr )
    screenlock
    put (0,0), wfbg_image, pset
    line (160,120)-(480,360),rgb(128,128,128),BF
    line (160,120)-(480,360),rgb(255,255,255),B
    draw string (320-(ext.gfx.font.getTextWidth(mfont,"Enter your name")/2),210), "Enter your name",,mfont,alpha
    draw string (320-(ext.gfx.font.getTextWidth(mfont,"for the High Scores")/2),230), "for the High Scores",,mfont,alpha
    draw string (x,y), s, , mfont, alpha
    screenunlock
end sub

''******game_over******
sub game_over( byref player as player_struct)
    dim particle(250) as particle_struct
    var pop_count = 1
    cls
    chdir exepath()
    dim as ext.xml.tree hs
    dim highs() as string

    dim highn() as ulongint
    redim highs(4)
    redim highn(4)

    hs.load(CONFIG_DIR & "highscores.dat")

    for n as integer = 0 to 4

        dim temp() as string
        highs(n) = hs.root->child("settings")->child("option",n)->child("options")->attribute("value")
        ext.strings.Explode(highs(n), ":", temp() )
        highn(n) = val(temp(ubound(temp)) & "ull")

    next

    Var beatit = 5

    for n as integer = 4 to 0 step -1

        if highn(n) < player.score then beatit = n

    next

    dim as uinteger ptr scr = screenptr
    dim as integer r,g,b
    dim as double a
    dim as ext.gfx.image ptr msci
    msci = ext.gfx.loadimage("../img/madscience.png")
    dim as integer mx, my, mbutt
    do
        getmouse mx, my,,mbutt
        a = timer

        if pop_count = 1 then

            for p_count as integer = 0 to 250

                var randang = rnd*pi2
                particle(p_count).p = vec2f(csng(186) + RndRange(-10.0,10.0),csng(226)+RndRange(-10.0,10.0)) '+vec2f( csng(mx)*Sin(randang), csng(my)*Cos(randang))
                particle(p_count).v = vec2f(RndRange(-10.0,10.0), RndRange(-10.0,10.0))
                particle(p_count).v.Normalize
                particle(p_count).v*=RndRange(0.0,5.0)

            next

        end if

        screenlock

        a = timer

        for y as integer = 0 to (240\4)-1

            var sy = y*8
            var ey = sy+7

            for x as integer = 0 to (320\4)-1

                var sx = x*8
                var ex = sx+7

                'ooooo trippy
                R = abs(64 + 96 * cos(a+x/(32+16*sin(y/70+a))))
                G = abs(64 + 96 * sin(a+y/(32-12*cos(x/100+a))))
                B = abs(64 + 96 * cos(a+(((R*sin(a))/128)^2 + ((G*cos(a))/128)^2)))

                for y2 as integer = sy to ey

                    for x2 as integer = sx to ex

                        scr[x2+y2*640] = rgb(R, G, B)

                    next

                next

            next

        next

        put (320-(msci->width/2),300-(msci->height/2)), msci, trans

        draw string (320-(ext.gfx.font.getTextWidth(mfont,"GAME OVER")/2),30),"GAME OVER",, mfont, alpha
        draw string (320-(ext.gfx.font.getTextWidth(mfont,"Final Score")/2),50), "Final Score",, mfont,alpha
        draw string (320-(ext.gfx.font.getTextWidth(mfont,str(player.score))/2),70), str(player.score),, mfont,alpha

        if beatit <> 5 then

            draw string (320-(ext.gfx.font.getTextWidth(mfont,"New High Score!")/2),90), "New High Score!",, mfont,alpha

        end if

        var tw = ext.gfx.font.getTextWidth(mfont,"Next")/2
        line (320-tw-5,400)-(330+(tw),420), rgb(128,128,128), BF
        draw string (320-tw,403), "Next",,mfont,alpha
        line (320-tw-5,400)-(330+(tw),420), rgb(255,0,0), B

        if pop_count>0 then

            for p as integer = 0 to ubound(particle)

                particle(p).p+=particle(p).v
                particle(p).v.y+=.1
                particle(p).v.x*=.995

                circle(particle(p).p.x, particle(p).p.y),5-pop_count/25,rgba(RndRange(10,255),RndRange(10,255),RndRange(10,255),255-(pop_count*2)),,,,f

            next

            pop_count+=1

            if pop_count>40 then pop_count = 1

        end if

        screensync 'vsync
        screenunlock

        var disposable_key = inkey


        if disposable_key = chr(27) then exit do
        if disposable_key = chr(255)+"k" then exit do

        if mbutt and 1 then
            if mx > 320-tw-5 andalso mx < 330+(tw) andalso my > 400 andalso my < 420 then
                exit do
            end if
        end if

        sleep 10,1

    loop

    cls

    if beatit <> 5 then

        var in = inputex()
        in._print_cb = @input_cb_gfx
        in.maxlength = 10

        var name_in = in.get(250, 250)
        if in.cretval <> 0 then return
        name_in = name_in & ": " & player.score

        'rotate the high scores
        if beatit = 4 then

            highs(4) = name_in

        else

            for n as integer = beatit to 4

                swap highs(n), name_in

            next

        end if

        'save
        for n as integer = 0 to 4

            hs.root->child("settings")->child("option",n)->child("options")->attribute("value") = highs(n)
        next

        hs.unload(CONFIG_DIR & "highscores.dat")

    end if


end sub

''******intro******
sub intro()

    dim play_img as ext.gfx.image ptr
    play_img = ext.gfx.loadimage("../img/play.png")
    var exit_img = ext.gfx.loadimage("../img/exit.png")
    var help_but = ext.gfx.loadimage("../img/help_button.png")

    dim particle(250) as particle_struct
    dim buf2 as ext.gfx.image ptr

    chdir exepath()
    var buf4 = ext.gfx.loadimage("../img/flag.png")

    dim as ext.xml.tree hs
    dim highs() as string

    dim highn() as ulongint
    redim highs(4)
    redim highn(4)
    dim as integer rclick

    hs.load(CONFIG_DIR & "highscores.dat")
    if hs.root->child(0) = null then 'create default high scores
        mkdir CONFIG_DIR
        hs.root->appendChild("settings")
        for n as integer = 0 to 4
            hs.root->child(0)->appendChild("option")->appendChild("options")
        next
        hs.root->child(0)->child(0)->child(0)->attribute("value") = "v1ctor:1000000"
        hs.root->child(0)->child(1)->child(0)->attribute("value") = "Dr_D:750000"
        hs.root->child(0)->child(2)->child(0)->attribute("value") = "sir_mud:500000"
        hs.root->child(0)->child(3)->child(0)->attribute("value") = "stylin:100000"
        hs.root->child(0)->child(4)->child(0)->attribute("value") = "LachieD:10"
        hs.unload(CONFIG_DIR & "highscores.dat")
    end if

    for n as integer = 0 to 4

        dim temp() as string
        highs(n) = hs.root->child("settings")->child("option",n)->child("options")->attribute("value")

    next

    var buf5 = imagecreate(320,240)

    draw string buf5,(160-len(ltrim(rtrim("HIGHSCORES")))*8,32),"HIGHSCORES",,mfont,trans

    for n as integer = 0 to 4

        draw string buf5,(160-len(ltrim(rtrim(highs(n))))*8,(n*16)+64),highs(n),,mfont,trans

    next

    ext.gfx.change_color(buf5,0,rgb(255,0,255))

    buf2 = imagecreate(1,240)
    var b_buf1 = imagecreate(1,64)

    var buf3 = imagecreate(640,480)

    ext.gfx.scale2x(buf3,*buf4,0,0)

    dim Ang as double = 0.0
    var pop_count = 1
    var timerx = timer
    dim as single randang
    var mx = 1, my = 1, mbutt = 0
    var idle = timer + 20

    var cheat = FSOUND_Sample_Load(FSOUND_FREE, "snd/fx/cheaters.ogg", FSOUND_HW2D or FSOUND_LOOP_OFF, 0, 0)
    var win = FSOUND_Sample_Load(FSOUND_FREE, "snd/fx/winners.ogg", FSOUND_HW2D or FSOUND_LOOP_OFF, 0, 0)

    do
        var hover = 0
        var keyb = inkey
        if keyb = chr(27) or keyb = chr(255)+"k" then system
        getmouse mx, my, ,mbutt

        if mx > 0 then

            if pop_count = 1 then

                for p_count as integer = 0 to 250

                    randang = rnd*pi2
                    particle(p_count).p = vec2f(csng(mx) + RndRange(-10.0,10.0),csng(my)+RndRange(-10.0,10.0)) '+vec2f( csng(mx)*Sin(randang), csng(my)*Cos(randang))
                    particle(p_count).v = vec2f(RndRange(-10.0,10.0), RndRange(-10.0,10.0))
                    particle(p_count).v.Normalize
                    particle(p_count).v*=RndRange(0.0,5.0)

                next

            end if

        end if

        Ang=timer*2

        screenlock

        cls
        put(0,0),buf3

        if timer - timerx > 10 then

            'swap buf, buf5
            timerx = timer

        end if


        for x as integer = 0 to 318

            Ang+=(6.282f/318)

            get buf5, (x,0)-(x+1,239), buf2

            put (x+170,(25*sin(Ang))+120), Buf2, trans

            if x < 127 andalso mx > 182 andalso my > 360 andalso mx < 310 andalso my < 424 then
            get play_img, (x,0)-(x+1,63), b_buf1
            put (x+(310-128),(25*sin(Ang))+360), b_buf1, trans
            hover = 1
            end if

            if x < 127 andalso mx > 330 andalso my > 360 andalso mx < 458 andalso my < 424 then
            get exit_img, (x,0)-(x+1,63), b_buf1
            put (x+(330),(25*sin(Ang))+360), b_buf1, trans
            hover = 2
            end if

            if x < 63 andalso mx > 576 andalso my > 416 andalso mx < 640 andalso my < 480 then
            get help_but, (x,0)-(x+1,63), b_buf1
            put (x+(576),(25*sin(Ang))+416), b_buf1, trans
            hover = 3
            end if

        next

        if hover <> 1 then put (320-(play_img->width+10),360), play_img, pset
        if hover <> 2 then put (330,360), exit_img, pset
        if hover <> 3 then put (576,416), help_but, trans

        if pop_count>0 then

            for p as integer = 0 to ubound(particle)

                particle(p).p+=particle(p).v
                particle(p).v.y+=.1
                particle(p).v.x*=.995

                circle(particle(p).p.x, particle(p).p.y),5-pop_count/25,rgba(RndRange(10,255),RndRange(10,255),RndRange(10,255),255-(pop_count*2)),,,,f

            next

            pop_count+=1

            if pop_count>60 then pop_count = 1

        end if

        screenunlock
        sleep 10,1

        if idle < timer then
            showHelp()
            idle = timer + 20
        end if

        if (mbutt and 2) then

          if mx > 238 andalso my > 444 andalso mx < 300 andalso my < 464 andalso rclick = 0 then

                var oldlimit = FSOUND_GetSFXMasterVolume( )
                FSOUND_SetSFXMasterVolume( 255 )

                if sm = 0 then
                    sm = 1
                    FSOUND_PlaySoundEx( FSOUND_FREE, cheat, 0, 0 )
                    sleep 50,1
                else
                    sm = 0
                    FSOUND_PlaySoundEx( FSOUND_FREE, win, 0, 0 )
                    sleep 50,1
                end if
                FSOUND_SetSFXMasterVolume( oldlimit )

          end if

            rclick = 1
         else
          rclick = 0
          if mbutt and 1 then

            if mx > 182 andalso my > 360 andalso mx < 310 andalso my < 424 then
                'play button
                sleep 50,1
                exit do
            end if

            if mx > 330 andalso my > 360 andalso mx < 458 andalso my < 424 then
                'exit button
                'TODO: go to cleanup function here
                'f*** that we'll let the OS handle it this one time while we make a sammich
                system
            end if

            if mx > 576 andalso my > 416 andalso mx < 640 andalso my < 480 then
                'help button
                sleep 50,1
                showHelp()
            end if

          end if

        end if

    loop

    FSOUND_Sample_free(cheat)
    FSOUND_Sample_free(win)
    imagedestroy buf5
    'imagedestroy buf4
    imagedestroy buf3
    imagedestroy buf2
    'imagedestroy play_img
    'imagedestroy exit_img
    imagedestroy b_buf1


end sub

sub showHelp()

    var help1 = ext.gfx.png.load("../img/help1.png")
    var help2 = ext.gfx.png.load("../img/help2.png")
    var help3 = ext.gfx.png.load("../img/help3.png")

    var page = 1
    var pagetimer = timer + 10

    do
        screenlock
        cls
        select case page
            case 1
                put(0,0), help1, pset
            case 2
                put(0,0), help2, pset
            case 3
                put(0,0), help3, pset
        end select
        screenunlock
        sleep 10,1


        if pagetimer < timer then
            pagetimer = timer + 10
            page += 1
        end if

        if page > 3 then exit do
        dim as integer mx, my, mbutt
        getmouse mx, my, , mbutt
        if mbutt > 0 then exit do
        if inkey <> "" then exit do
        if FSOUND_IsPlaying( song_channel ) = 0 then song_channel = playNextSong
    loop

    'imagedestroy help1
    'imagedestroy help2
    'imagedestroy help3
    setmouse 320, 240

end sub
